package intelligentCustomer.planning;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 计划执行结果
 * 存储计划执行的最终结果和详细信息
 */
public class PlanResult {
    private final Map<String, String> results;
    private final boolean successful;
    private final String executionLog;
    
    public PlanResult(Map<String, String> results, boolean successful) {
        this(results, successful, "");
    }
    
    public PlanResult(Map<String, String> results, boolean successful, String executionLog) {
        this.results = new HashMap<>(results);
        this.successful = successful;
        this.executionLog = executionLog;
    }
    
    /**
     * 获取最终响应
     */
    public String getFinalResponse() {
        // 如果有最终响应步骤的结果，直接返回
        if (results.containsKey("最终响应") || results.containsKey("生成回复")) {
            return results.getOrDefault("最终响应", results.getOrDefault("生成回复", ""));
        }
        
        // 否则整合所有结果
        return results.values().stream()
                .filter(s -> s != null && !s.isEmpty())
                .collect(Collectors.joining("\n\n"));
    }
    
    /**
     * 获取执行详情
     */
    public String getExecutionDetails() {
        StringBuilder details = new StringBuilder();
        details.append("计划执行状态: ").append(successful ? "成功" : "失败").append("\n\n");
        
        if (!executionLog.isEmpty()) {
            details.append("执行日志:\n").append(executionLog).append("\n\n");
        }
        
        details.append("步骤结果:\n");
        for (Map.Entry<String, String> entry : results.entrySet()) {
            details.append("- ").append(entry.getKey()).append(":\n");
            // 限制结果长度，避免过长
            String value = entry.getValue();
            if (value.length() > 100) {
                value = value.substring(0, 97) + "...";
            }
            details.append("  ").append(value.replace("\n", "\n  ")).append("\n");
        }
        
        return details.toString();
    }
    
    /**
     * 获取特定步骤的结果
     */
    public String getStepResult(String stepName) {
        return results.getOrDefault(stepName, "");
    }
    
    /**
     * 检查计划是否成功执行
     */
    public boolean isSuccessful() {
        return successful;
    }
    
    /**
     * 获取所有结果
     */
    public Map<String, String> getAllResults() {
        return new HashMap<>(results);
    }
} 